package com.operate.api.app.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.operate.api.app.user.domain.vo.*;
import com.operate.api.app.user.service.IAppUserService;
import com.operate.api.base.ApiIndex;
import com.operate.api.base.IDEntity;
import com.operate.api.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@Api( tags = ApiIndex.AppUserController)
@RequestMapping(value = "/api/app/user")
@RestController
@Slf4j
public class AppUserController {

    @Autowired
    IAppUserService service;

    @ApiOperation(value = "短信码登录(首次登录会自动注册一个用户)")
    @PostMapping(value = "/login_code")
    public R<AppUserLoginResVO> login_code(@ApiParam(value = "参数")  @RequestBody AppUserPhoneCodeVO vo) {
        AppUserLoginResVO resVO=service.loginCode(vo);
        return new R(resVO);
    }

    @ApiOperation(value = "账户密码登录")
    @PostMapping(value = "/login")
    public R<AppUserLoginResVO> login(@ApiParam(value = "参数")  @RequestBody AppUserLoginReqVO vo) {
        AppUserLoginResVO resVO=service.login(vo);
        return new R(resVO);
    }

    @ApiOperation(value = "账号退出登录")
    @PostMapping(value = "/login_out")
    public R login_out() {
        service.loginOut();
        return new R();
    }

    @ApiOperation(value = "检查是否登录(token是否有效)")
    @GetMapping(value = "/check_login")
    public R check_login() {
        return new R(StpUtil.isLogin());
    }

    @ApiOperation(value = "发送验证码")
    @PostMapping(value = "/send_code")
    public R sendCode(@ApiParam(value = "参数")  @RequestBody AppCodeVO vo) {
         service.sendCode(vo);
        return new R();
    }

    @ApiOperation(value = "验证短信码【验证完后验证码会失效】")
    @PostMapping(value = "/check_code")
    public R<AppUserLoginResVO> check_code(@ApiParam(value = "参数")  @RequestBody AppUserPhoneCodeVO vo) {
        service.checkCode(vo);
        return new R();
    }

    @ApiOperation(value = "设置/找回密码")
    @PostMapping(value = "/reset_password")
    public R<AppUserLoginResVO> reset_password(@ApiParam(value = "参数")  @RequestBody AppUserPasswordVO vo) {
        service.resetPassword(vo);
        return new R();
    }



    @ApiOperation(value = "开启青少年模式")
    @PostMapping(value = "/open_young")
    @SaCheckLogin
    public R open_young(@ApiParam(value = "参数")  @RequestBody AppUserYoungPasswordVO vo) {
        service.open_young_password(vo);
        return new R();
    }


    @ApiOperation(value = "关闭青少年模式")
    @PostMapping(value = "/close_young")
    @SaCheckLogin
    public R close_young(@ApiParam(value = "参数")  @RequestBody AppUserYoungPasswordVO vo) {
        service.close_young_password(vo);
        return new R();
    }


    @ApiOperation(value = "验证青少年模式密码（开启青少年模式的时候需要验证）")
    @PostMapping(value = "/check_young_password")
    @SaCheckLogin
    public R check_young_password(@ApiParam(value = "参数")  @RequestBody AppUserYoungPasswordVO vo) {
        service.check_young_password(vo);
        return new R();
    }


    @ApiOperation(value = "得到登录人信息[需要登录]")
    @GetMapping(value = "/one")
    @SaCheckLogin
    public R<AppUserVO> getOne() {
        AppUserVO resVO=service.getOne();
        return new R(resVO);
    }

    @ApiOperation(value = "更换登录人手机[需要登录]")
    @PostMapping(value = "/update_phone")
    @SaCheckLogin
    public R update_phone(@ApiParam(value = "参数")  @RequestBody AppUserPhoneCodeVO vo) {
        service.updatePhone(vo);
        return new R();
    }


    @ApiOperation(value = "注销账号")
    @PostMapping(value = "/log_off")
    @SaCheckLogin
    public R logOff() {
        service.logOff();
        return new R();
    }


    @ApiOperation(value = "修改登录人密码[需要登录]")
    @PostMapping(value = "/update_password")
    @SaCheckLogin
    public R<AppUserLoginResVO> update_password(@ApiParam(value = "参数")  @RequestBody AppUserUpdatePasswordVO vo) {
        service.updatePassword(vo);
        return new R();
    }



}